package com.huidu.bitrade.service;

import cn.hutool.core.lang.Assert;
import com.huidu.bitrade.constant.CommonStatus;
import com.huidu.bitrade.constant.DepositStatusEnum;
import com.huidu.bitrade.constant.PageModel;
import com.huidu.bitrade.dao.BusinessDepositStrategyDao;
import com.huidu.bitrade.entity.BusinessDepositStrategy;
import com.huidu.bitrade.entity.*;
import com.huidu.bitrade.service.Base.BaseService;
import com.huidu.bitrade.vo.BusinessDepositStrategyVo;
import com.querydsl.core.types.Predicate;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author QQ：97095646
 * @date 2019/5/5
 */
@Service
public class BusinessDepositStrategyService extends BaseService {

    @Autowired
    private BusinessDepositStrategyDao businessAuthDepositDao;
    @Autowired
    private MemberWalletService memberWalletService;
    @Autowired
    private DepositRecordService depositRecordService;

    @Override
    public Page<BusinessDepositStrategy> findAll(Predicate predicate, PageModel pageModel) {
        if(Objects.isNull(predicate)){
            return businessAuthDepositDao.findAll(pageModel.getPageable());
        }
        return businessAuthDepositDao.findAll(predicate, pageModel.getPageable());
    }

    public List<BusinessDepositStrategy> findAllByStatus(CommonStatus status){
        return businessAuthDepositDao.findAllByStatus(status);
    }

    public BusinessDepositStrategy findById(Long id){
        return businessAuthDepositDao.findById(id).orElse(null);
    }

    public void save(BusinessDepositStrategy strategy){
        businessAuthDepositDao.save(strategy);
    }

    public void update(BusinessDepositStrategy strategy){
        businessAuthDepositDao.save(strategy);
    }

    /**
     * 获取保证金策略
     * @return
     */
    public List<BusinessDepositStrategyVo> getDepositStrategyVo(Member member){
        List<BusinessDepositStrategyVo> result = new ArrayList<>();
        List<BusinessDepositStrategy> list = findAllByStatus(CommonStatus.NORMAL);
        if(CollectionUtils.isEmpty(list)){
            return result;
        }
        list = list.stream().sorted(Comparator.comparing(BusinessDepositStrategy::getBusinessLevelEnum)).collect(Collectors.toList());
        //查询账户余额
        MemberWallet memberWallet = memberWalletService.findByCoinUnitAndMemberId("CNHD",member.getId());
        //查询交保证金记录
        BigDecimal payPrice = BigDecimal.ZERO;
        List<DepositRecord> records = depositRecordService.findByMemberAndStatusAndType(member,DepositStatusEnum.PAY,1);
        if(CollectionUtils.isNotEmpty(records)){
            payPrice = records.get(0).getAmount();
        }
        for (BusinessDepositStrategy strategy:list){
            result.add(BusinessDepositStrategyVo.builder()
                    .amount(strategy.getAmount())
                    .businessAuthDepositId(strategy.getId())
                    .dailyAmount(strategy.getDailyAmount())
                    .residueAmount(payPrice.compareTo(strategy.getAmount())>=0?null:strategy.getAmount().subtract(payPrice))
                    .balance(Objects.isNull(memberWallet)?BigDecimal.ZERO:memberWallet.getBalance())
                    .businessLevelEnum(strategy.getBusinessLevelEnum()).build());
        }
        return result;
    }
}
